﻿using Microsoft.AspNetCore.Mvc;
using Qy.Common;
using Qy.Models;
using Qy.Service;
using Qy.Web.Authorize;
using System.Threading.Tasks;

namespace Qy.Web.Api
{
    [Route("Api/[controller]/[action]")]
    [Verify]
    public class UserPostController : Base
    {
        private readonly UserPostService _UserPostService;

        public UserPostController(UserPostService adminUserPostService)
        {
            _UserPostService = adminUserPostService;
        }


        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.UserPost_Index)]
        public async Task<IActionResult> IndexAsync()
        {
            var pagedList = await _UserPostService.GetAllAsync();
            return Json(new { status = 1, data = pagedList });
        }

        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.UserPost_AddOrEditView)]
        public async Task<IActionResult> AddOrEditAsync(int id)
        {
            var data = await _UserPostService.GetInfo(id);
            return Json(new { status = 1, data });
        }

        [HttpPost, ActionName("Create")]
        [XssFieldFilter]//防Xss
        [ActionPermissionFilter(Permission = PermissionKeys.UserPost_AddOrEdit)]
        [Log(Name = "新增岗位")]
        public async Task<IActionResult> CreatePostAsync([FromBody] UserPost userPost)
        {
            if (ModelState.IsValid)
            {
                object insertPri = await _UserPostService.InsertAsync(userPost);
                if (insertPri != null)
                    return Json(new { status = 1, msg = "新增岗位 ID:" + insertPri.ToString() });
                else
                    return Json(new { status = 0, msg = "新增岗位 错误，请查看控制台日志！" });
            }
            else
            {
                var error = ModelState.GetErrorMessages();
                return Json(new { status = 0, msg = error });
            }
        }

        [HttpPut, ActionName("Edit")]
        [XssFieldFilter]//防Xss
        [ActionPermissionFilter(Permission = PermissionKeys.UserPost_AddOrEdit)]
        [Log(Name = "修改岗位")]
        public async Task<IActionResult> EditPostAsync([FromBody]EditUserPost userPost, int dyEdit = 0)
        {

            if (ModelState.IsValid)
            {
                UserPost adminUserDept = await _UserPostService.GetInfo(userPost.Id);
                if (dyEdit == 1)
                {
                    adminUserDept.Sort = userPost.Sort;

                    if (0 < await _UserPostService.UpdateAsync(adminUserDept))
                        return Json(new { status = 1, msg = "修改岗位排序:" + adminUserDept.PostName + " Id： " + userPost.Id });
                    else
                        return Json(new { status = 0, msg = "修改岗位排序失败:" + adminUserDept.PostName + " Id： " + userPost.Id });

                }

                adminUserDept.PostName = userPost.PostName;
                adminUserDept.PostCode = userPost.PostCode;
                adminUserDept.Sort = userPost.Sort;
                adminUserDept.Remark = userPost.Remark;
                if (0 < await _UserPostService.UpdateAsync(adminUserDept))
                    return Json(new { status = 1, msg = "修改岗位:" + adminUserDept.PostName + " Id： " + userPost.Id });
                else
                    return Json(new { status = 0, msg = "修改岗位:" + adminUserDept.PostName + " Id： " + userPost.Id });
            }
            else
            {
                var error = ModelState.GetErrorMessages();
                return Json(new { status = 0, msg = error });
            }
        }

        [HttpDelete, ActionName("Delete")]
        [ActionPermissionFilter(Permission = PermissionKeys.UserPost_Delete)]
        [Log(Name = "删除岗位")]
        public async Task<IActionResult> DeleteAsync(int id)
        {
            if (await _UserPostService.DeleteByPriKeyAsync(id) > 0)
                return Json(new { status = 1, msg = "删除岗位,Id： " + id });
            else
                return Json(new { status = 0, msg = "删除失败,Id： " + id });
        }

    }
}
